<!DOCTYPE html>
<html class="ui-page-login">

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<meta name="format-detection" content="telephone=no,email=no" />
		<title>绑定账号</title>
		<link href="css/mui.min.css" rel="stylesheet" />
		<link href="css/style.css" rel="stylesheet" />
		<style>
			.area {
				margin: 20px auto 0px auto;
			}
			.mui-input-group {
				margin-top: 10px;
			}
			.mui-input-group:first-child {
				margin-top: 20px;
			}
			.mui-input-group label {
				width: 22%;
				line-height: 21px;
			}
			.mui-input-row label~input,
			.mui-input-row label~select,
			.mui-input-row label~textarea {
				width: 78%;
			}
			.mui-checkbox input[type=checkbox],
			.mui-radio input[type=radio] {
				top: 6px;
			}
			.mui-content-padded {
				margin-top: 25px;
			}
			.mui-btn {
				padding: 10px;
			}
			.link-area {
				display: block;
				margin-top: 15px;
				text-align: right;
				font-size: 14px;
			}
		</style>

	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">绑定账号</h1>
		</header>
		<div class="mui-content">
			<form id='login-form' class="mui-input-group">
				<div class="mui-input-row">
					<label>账号</label>
					<input id='account' type="text" class="mui-input-clear mui-input" placeholder="请输入账号">
				</div>
				<div class="mui-input-row mui-password">
					<label>密码</label>
					<input id='password' type="password" class="mui-input-password mui-input" placeholder="请输入密码">
				</div>
			</form>
			<div class="mui-content-padded">
				<button id='login' class="mui-btn mui-btn-block mui-btn-danger">立即绑定</button>
				<div class="link-area"><a id='noBind'>不绑定直接登录 <span class="mui-icon mui-icon-arrowthinright"></span></a></div>
			</div>
		</div>
		<script src="js/mui.min.js"></script>
		<script src="js/mui.enterfocus.js"></script>
		<script src="js/app.js"></script>
		<script>
			(function($, doc) {
				$.init({
					swipeBack:true
				});
				$.plusReady(function() {
					var dataAuth=plus.webview.currentWebview().dataAuth,
						dataAuthArr=JSON.parse(dataAuth),
						dataResult=dataAuthArr,
						loginPage=plus.webview.getWebviewById('login.html');
					var toMain = function() {
						mui.fire(plus.webview.getWebviewById('user/usercenter.html'),'updateData');
						plus.webview.currentWebview().close();
					};
					
					var loginBtn = doc.getElementById('login'),
						accountBox = doc.getElementById('account'),
						passwordBox = doc.getElementById('password'),
						noBindBtn = doc.getElementById('noBind');
					loginBtn.addEventListener('tap', function() {
						if (accountBox.value.length < 4) {
							plus.nativeUI.toast('账号最短为 4 个字符');
							return false;
						}
						if (passwordBox.value.length < 6) {
							plus.nativeUI.toast('密码最短为 6 个字符');
							return false;
						}
						dataResult=mui.extend({userName:accountBox.value,password:passwordBox.value},dataAuthArr);
						regLogin(dataResult);
					});
					
					noBindBtn.addEventListener('tap', function() {
						regLogin(dataResult);
					});
					
					function regLogin(dataResult){
						var w=plus.nativeUI.showWaiting('',{back:'none'});
						$.ajax(URL+'api/Register/PostRegisterUser',{
							data:JSON.stringify(dataResult),
							dataType:'json',
							type:'POST',
							contentType:'application/json',
							timeout:10000,
							success:function(data){
								if(data.Success=="true"){
									loginPage.hide();
									loginPage.close();
									$.ajax(URL+'api/Login/GetUser',{
										data:dataAuthArr,
										dataType:'json',
										type:'get',
										timeout:10000,
										success:function(data){
											w.close();
											if(data.Success=="true"){
												plus.nativeUI.toast("登录成功");
												return app.createState(dataAuthArr.oauthNickName,data.UserId,function(){
													toMain();
												});
											}
										},
										error:function(xhr,type,errorThrown){
											w.close();
											plus.nativeUI.toast('请求失败，请检查网络')
										}
									});
								}else{
									w.close();
									plus.nativeUI.toast('绑定超时，请重新绑定')
								}
							},
							error:function(xhr,type,errorThrown){
								w.close();
								plus.nativeUI.toast('请求失败，请检查网络')
							}
						});
					}
				});
			}(mui, document));
		</script>
	</body>

</html>
